/**
 * 自定义公共函数
 */
var myfn = {};

// 用户授权登录
myfn.userLogin = async function(code = '',info = {}) {
	var that = this
	uni.showLoading({
		mask: true,
		title: '登录中...'
	});
	let data = await uni.vk.callFunction({
		// #ifdef MP-WEIXIN
		url: 'user/pub/loginByWeixin',
		// #endif
		data:{
			code,
			inviteCode: uni.getStorageSync('inviteId')?uni.getStorageSync('inviteId'):'',
			nickname: info.nickName,
			gender: info.gender,
			avatarUrl: info.avatarUrl
		}
	});
	if(data.code === 0){
		uni.removeStorageSync('inviteId')
		if(data.register) that.upScore(data.userInfo.inviter_uid, "invited", "邀请用户 "+data.userInfo.nickname)
	}
	uni.hideLoading()
	uni.showToast({
		icon: 'none',
		mask: true,
		title: data.msg,
		duration: 1500
	})
	return data
};
// 用户更新积分
myfn.upScore = async function(_id,type,comment) {
	let data = await uni.vk.callFunction({
		url: 'client/wechat/list/kh/upScore',
		data:{
			_id,
			type,
			comment
		}
	});
	uni.vk.setVuex('$user.userInfo.score', data.user.score)
	return data
};

// 判断下载类型
myfn.downModel = function(downMode, adCount) {
	var that = this
	let model = {}
	let downFlag = uni.vk.getVuex('$setting.client.'+downMode+'_flag')
	let downScore = uni.vk.getVuex('$setting.client.'+downMode+'_score')
	
	if(uni.vk.getVuex('$user.userInfo.vip_time') > new Date().getTime()){
		model.type = 'vip'
		model.title = "尊贵的VIP用户，您享有特权，当前作品无需付费即可免费下载！"
		model.btn = "VIP免费下载"
		model.close = "再想想看"
		model.ad = false
		model.download = false
		model.show = true
	}else if(downFlag === 1){
		var score = uni.vk.getVuex('$user.userInfo.score')
		if(score < downScore){
			model.type = 'video'
			model.title = "<div style='padding-top: 10px;'>积分不足，下载高清原图需要<span class='num' style='color: "+uni.vk.getVuex('$app.config.color.sec')+"'>"+downScore+"</span>"+uni.vk.getVuex('$setting.score.name')+"，你当前仅有<span class='num' style='color: "+uni.vk.getVuex('$app.config.color.sec')+"'>"+score+"</span>"+uni.vk.getVuex('$setting.score.name')+"，点击按钮快速赚"+uni.vk.getVuex('$setting.score.name')+"~</div>"
			model.btn = "看广告领 "+uni.vk.getVuex('$setting.score.video_score')+" "+uni.vk.getVuex('$setting.score.name')
			model.close = "再想想看"
			model.download = false
		}else{
			model.type = 'down'
			model.title = "<div style='padding-top: 10px;'>下载高清原图需要<span class='num' style='color: "+uni.vk.getVuex('$app.config.color.sec')+"'>"+downScore+"</span>"+uni.vk.getVuex('$setting.score.name')+"，当前有<span class='num' style='color: "+uni.vk.getVuex('$app.config.color.sec')+"'>"+score+"</span>"+uni.vk.getVuex('$setting.score.name')+"，点击按钮立即下载！</div>"
			model.btn = downMode==='downs'?"超清视频下载":"超清原图下载"
			model.close = "再想想看"
			model.download = false
		}
		model.ad = false
		model.show = true
	}else if(downFlag === 2){
		model.type = 'ad'
		model.title = "看段小广告，广告结束自动下载高清原图，普通下载无需观看广告！"
		model.btn = downMode==='downs'?"超清视频下载":"超清下载"
		model.ad = false
		model.download = downMode==='downs'?false:true
		model.show = true
	}
	return model;
};

export default myfn;
